﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MODULE_DANHMUC
{
   
    public partial class frm_danhmuchanghoa : Form
    {
         int currenrowindex;
         danhmucDataContext context = cls_danhmuc.getcontext();
        public frm_danhmuchanghoa()
        {
            InitializeComponent();
        }

        private void butSave_Click(object sender, EventArgs e)
        {
            //danhmucDataContext context = new danhmucDataContext();
            
            DM_hanghoa hh = new DM_hanghoa();
            hh.mahang = this.txtmahang.Text;
            hh.tenhang = this.txttenhang.Text;
           
           try
           {
             
              
                    IEnumerable<DM_hanghoa> l1=from c in context.DM_hanghoas where c.mahang.Contains(hh.mahang) select c;
               if(l1.Count()>0)
               {       
                   foreach( DM_hanghoa hhold in l1)
                   {
                       hhold.tenhang = hh.tenhang;
                       
                   }

               }
               else
               {
                   context.DM_hanghoas.InsertOnSubmit(hh);
               }
           
            context.SubmitChanges();
                  MessageBox.Show("Lưu thành công", "Thông báo", MessageBoxButtons.OK);
                 
            }
            catch (Exception ex)
            {
                MessageBox.Show("Không lưu được.Lỗi: "+ex.Message, "Thông báo", MessageBoxButtons.OK);
            }
           getdata();
        }

        private void getdata()
        {
            this.grdata.Rows.Clear();
           
            // this.grdata.DataSource = context.DM_hanghoas;
            IEnumerable<DM_hanghoa> list = context.DM_hanghoas.AsEnumerable();
            for (int i = 0; i < list.Count(); i++)
            {
               DM_hanghoa hh = list.ElementAt(i);
               this.grdata.Rows.Add();
                
               this.grdata.Rows[i].Cells["mahang"].Value = hh.mahang;
               this.grdata.Rows[i].Cells["tenhang"].Value = hh.tenhang;
               


           }
           
        }
        private void frm_danhmuchanghoa_Load(object sender, EventArgs e)
        {
           

            currenrowindex = 0;
            getdata();
           
           

        }
        private void getrowdata()
        {
            DM_hanghoa hh = new DM_hanghoa();
             //danhmucDataContext context = new danhmucDataContext();
             if (currenrowindex < this.grdata.Rows.Count )
             {
                 string mahang = this.grdata.Rows[currenrowindex].Cells["mahang"].Value.ToString();
                 IEnumerable<DM_hanghoa> list = from c in context.DM_hanghoas where c.mahang.Equals(mahang) select c;

                 hh = list.ElementAt(0);
                 //hh.mahang = this.grdata.Rows[currenrowindex].Cells[0].Value == null ? "" : this.grdata.Rows[currenrowindex].Cells[0].Value.ToString();
                 //hh.tenhang = this.grdata.Rows[currenrowindex].Cells[1].Value == null ? "" : this.grdata.Rows[currenrowindex].Cells[1].Value.ToString();
                 this.txtmahang.Text = hh.mahang;
                 this.txttenhang.Text = hh.tenhang;
             }
             else
             {
                 clear(); 
             }
        }

        private void clear()
        {
            this.txtmahang.Text = "";
            this.txttenhang.Text = "";
        }
        private void grdata_Click(object sender, EventArgs e)
        {

            currenrowindex = this.grdata.CurrentRow.Index;
            this.grdata.Rows[currenrowindex].Selected = true;
            getrowdata();
          
        }

        private void btnext_Click(object sender, EventArgs e)
        {
            this.grdata.Rows[currenrowindex].Selected = false;
            if (currenrowindex < this.grdata.Rows.Count - 1)
            {
                currenrowindex = currenrowindex + 1;
            }

            this.grdata.Rows[currenrowindex].Selected = true;

            getrowdata();

        }

        private void btprevious_Click(object sender, EventArgs e)
        {
            this.grdata.Rows[currenrowindex].Selected = false;
            if (currenrowindex >0)
            {
                currenrowindex = currenrowindex - 1;
            }

            this.grdata.Rows[currenrowindex].Selected = true;

            getrowdata();
        }

        private void btlast_Click(object sender, EventArgs e)
        {
            this.grdata.Rows[currenrowindex].Selected = false;
          
                currenrowindex = this.grdata.Rows.Count-1;
           

            this.grdata.Rows[currenrowindex].Selected = true;

            getrowdata();
        }

        private void btfist_Click(object sender, EventArgs e)
        {
            this.grdata.Rows[currenrowindex].Selected = false;

            currenrowindex = 0;


            this.grdata.Rows[currenrowindex].Selected = true;

            getrowdata();
        }

        private void btDelete_Click(object sender, EventArgs e)
        { 
            try
            {
            //danhmucDataContext context = new danhmucDataContext();
            DM_hanghoa hh = new DM_hanghoa();

            hh.mahang = this.grdata.Rows[currenrowindex].Cells[0].Value == null ? "" : this.grdata.Rows[currenrowindex].Cells[0].Value.ToString();
            hh.tenhang = this.grdata.Rows[currenrowindex].Cells[1].Value == null ? "" : this.grdata.Rows[currenrowindex].Cells[1].Value.ToString();
            IEnumerable<DM_hanghoa> lh = from c in context.DM_hanghoas where c.mahang.Equals(hh.mahang) select c;

            
            
           // MessageBox.Show("Bạn chắc chắn muốn xóa?", "Xác nhận",MessageBoxButtons.YesNo);
            
 



            foreach (DM_hanghoa h in lh)
            {
                context.DM_hanghoas.DeleteOnSubmit(h);
            }
            context.SubmitChanges();

                   MessageBox.Show("Xóa thành công", "Thông báo", MessageBoxButtons.OK);
                 
            }
            catch (Exception ex)
            {
                MessageBox.Show("Không xóa được.Mã hàng đã đưa vào sử dụng");
                //MessageBox.Show("Không xóa được.Lỗi: "+ex.Message, "Thông báo", MessageBoxButtons.OK);
            }
           getdata();
        }

        private void btAdd_Click(object sender, EventArgs e)
        {
            this.txtmahang.Text = "";
            this.txttenhang.Text = "";

        }

        private void txtkeyword_KeyDown(object sender, KeyEventArgs e)
        {
           

        }

        private void txtkeyword_TextChanged(object sender, EventArgs e)
        {
            this.tabControl1.SelectedIndex = 1;
            string keyword = txtkeyword.Text;
            this.grdata.Rows.Clear();

            // this.grdata.DataSource = context.DM_hanghoas;
            IEnumerable<DM_hanghoa> list = from c in context.DM_hanghoas where c.mahang.Contains(keyword) select c;//context.DM_hanghoas.AsEnumerable();
            for (int i = 0; i < list.Count(); i++)
            {
                DM_hanghoa hh = list.ElementAt(i);
                this.grdata.Rows.Add();

                this.grdata.Rows[i].Cells["mahang"].Value = hh.mahang;
                this.grdata.Rows[i].Cells["tenhang"].Value = hh.tenhang;



            }
            IEnumerable<DM_hanghoa> list1 = from c in context.DM_hanghoas where c.mahang.Contains(keyword) select c;//context.DM_hanghoas.AsEnumerable();
            for (int j = 0; j < list1.Count(); j++)
            {
                DM_hanghoa hh = list1.ElementAt(j);
                this.grdata.Rows.Add();

                this.grdata.Rows[j].Cells["mahang"].Value = hh.mahang;
                this.grdata.Rows[j].Cells["tenhang"].Value = hh.tenhang;



            }

        }

        private void btExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

       

       
        
       
        




    }
}
